package com.ld.security.auth.server.modules.ge.api;

import com.ld.security.auth.server.modules.ge.entity.GeModelTel;
import com.ld.security.auth.server.modules.ge.service.IGeModelService;
import com.ld.security.auth.server.modules.ge.service.IGeModelTelService;
import com.ld.security.common.mybatis.base.po.IdsPo;
import com.ld.security.common.core.base.vo.DataVo;
import com.ld.security.common.core.base.vo.ListVo;
import com.ld.security.common.core.base.vo.PageVo;
import com.ld.security.common.core.util.TemplateUtil;
import com.ld.security.common.mybatis.base.entity.TreeEntity;
import com.ld.security.common.tomcat.base.api.SuperApi;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@Api(tags="VIEWApi管理")
@RequestMapping("/api/geModelTel")
public class  GeModelTelApi extends SuperApi<GeModelTel> {

    @Autowired
    private IGeModelService iGeModelService;

    @Autowired
    private IGeModelTelService iGeModelTelService;

    @RequestMapping(value = "/getTree", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation("得到树")
    public DataVo<PageVo<NodeTree>> getTree() {
        Map<String,List<GeModelTel>> map =  iGeModelTelService.all().stream()
                .collect(Collectors.groupingBy(val -> {
           return  val.getTableSchema();
        }));

        LinkedList<NodeTree> list =  new LinkedList<>();

        map.forEach((x,y) -> {
            NodeTree parent = new   NodeTree();
            parent.setId(x.replace("_","-"));
            parent.setLabel(parent.getId());
            List<NodeTree> children =  y.stream().map(val -> {
                NodeTree node = new   NodeTree();
                node.setId(     TemplateUtil.columnToJava(val.getTableName()));
                node.setLabel(val.getTableComment());
                return node;
            }).collect(Collectors.toList());
            parent.setChildren(children);
            list.add(parent);
        });

        DataVo dataVo =  DataVo.of(ListVo.of(list));
        return dataVo;
    }

    @RequestMapping(value="/initModelByTelId",method = RequestMethod.POST)
    @ApiOperation("初始化模型")
    public DataVo initModelByTelId(@RequestBody IdsPo po){
        iGeModelService.initModel(po);
        DataVo vo =DataVo.of();
        vo.setMessage("初始化模型模型成功");
        return vo;
    }


    static class NodeTree extends TreeEntity<NodeTree> {}


}
